package com.jh3502.vivo.function.author;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.function.author.add.AuthorNewActivity;
import com.jh3502.vivo.utils.Utils;
import com.jh3502.vivo.view.MyRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author harisucici
 * @version 2018/12/21 10:01
 * name
 * todo
 * @email harisucici@gmail.com
 */
public class AuthorListActivity extends BaseActivity<AuthorPresenter.AuthorView, AuthorPresenter> implements AuthorPresenter.AuthorView {

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.author_list)
    MyRecyclerView authorList;
    private boolean isEdit = false;
    private AuthorListAdapter mAdapter;
    private List<Integer> ids = new ArrayList<>(2);
    private List<AuthorListBean> dataList = new ArrayList<>(2);

    @Override
    public AuthorPresenter initPresenter() {
        return new AuthorPresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_author_list;
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        toolbarTitle.setText("委托授权");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mPresenter.setAuthorView(this);
        mAdapter = new AuthorListAdapter(this, dataList);
        mAdapter.setButtonSetOnclickDelete(position -> {
            ids.clear();
            ids.add(dataList.get(position).getId());
            Map<String, Object> body = new HashMap<>(2);
            body.put("ids", ids);
            mPresenter.toDelEntrust(body);

        });

        mAdapter.setButtonSetOnclickSwitch(position -> {
            Map<String, Object> body = new HashMap<>(2);
            int status = dataList.get(position).getStatus();
            if (status == 0) {
                status = 1;
            } else {
                status = 0;
            }
            body.put("id", dataList.get(position).getId());
            body.put("status", status);
            mPresenter.toEnableEntrust(body);
        });
        authorList.setAdapter(mAdapter);

    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (isEdit) {
            getMenuInflater().inflate(R.menu.menu_done, menu);
        } else {
            getMenuInflater().inflate(R.menu.menu_edit, menu);
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case R.id.action_edit:
                isEdit = true;
                setSupportActionBar(toolbar);
                mAdapter.setIsEdit(isEdit);
                mAdapter.notifyDataSetChanged();
                break;
            case R.id.action_done:
                isEdit = false;
                setSupportActionBar(toolbar);
                mAdapter.setIsEdit(isEdit);
                mAdapter.notifyDataSetChanged();
                break;
            default:
        }
        return true;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {
        mPresenter.toFindMyEntrust();
    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {

    }

    @Override
    public void findPermissListSuccess(String s) {

    }

    @Override
    public void submitEntrustSuccess() {

    }

    @Override
    public void findMyEntrustSuccess(List<AuthorListBean> datas) {
        dataList.clear();
        dataList.addAll(datas);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void delEntrustSuccess() {
        showShortToast("删除成功");
        mPresenter.toFindMyEntrust();
    }

    @Override
    public void enableEntrustSuccess() {
        showShortToast("操作成功");
        mPresenter.toFindMyEntrust();
    }


    @OnClick(R.id.author_add)
    public void onAuthorAddClicked() {
        Utils.getInstance().startActivity(this, AuthorNewActivity.class);
    }


}
